home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Cracking / Crack-It-Up.sit / Crack-It-Up / Crack-It-Up.rsrc / TEXT_139.txt < prev    next >
Text File  |  2000-01-29  |  17KB  |  331 lines

  1.                    Cool MacsBug Tricks (an informal guide)
  2.                               By Macneil Shonle
  3.  
  4. ----------------------------------------------------------------------------
  5. This guide is to help you in learning to use MacsBug. MacsBug is a system
  6. extension that can help you debug your programs, it is free and is available
  7. from Apple Computer. Note that the name MacsBug is an acronym for Motorola
  8. advanced computer systems debuger.
  9.  
  10. This guide is a list of "tricks," but it is just the tip of the iceberg of
  11. the cool stuff you can do. The guide will start off with some easy topics
  12. aimed towards beginners and then it will go into some more advanced topics.
  13. All of them are cool. Hopefully after reading this the help part of MacsBug
  14. won't be so intimidating.
  15.  
  16. Number Conversion
  17. MacsBug can be used as a quick hexadecimal to decimal converter, and vise
  18. versa. I used to use a calculator DA, but now I just simply drop into
  19. MacsBug and type in the number I want to convert and hit return.
  20.  
  21. Example: You want to find out what 0x3E is in decimal. When in MacsBug, type
  22. in $3E and hit return. This will be the output:
  23.  
  24. $3E = $0000003E #62 #62 '***>'
  25.  
  26. The first number ($0000003E) is the value in hexadecimal that you just typed
  27. in. The second number is what the value is as an unsigned decimal, the third
  28. is the signed version. The set of characters in single quotes ('***>') is
  29. the ASCII representation of the number, the null character is denoted with
  30. the bullet.
  31.  
  32. You can find out the decimal/hexadecimal equivalent of any ASCII character
  33. by typing the letter balanced between two single quotes.
  34.  
  35. Example: Type in: 'A' and hit return. You will get #65 as your answer.
  36.  
  37. By the way-The dollar sign means that the number is in hexadecimal. Much
  38. like C's 0xXX notation, $XX is how hexadecimal numbers are represented in
  39. assembly. Numbers in MacsBug will default to hexadecimal, except for when
  40. the hexadecimal number is a command or a regster. For example: "ea" is the
  41. command to restart the current application, when you type in ea it will try
  42. this command, you must type in $ea in order to avoid this conflict.
  43.  
  44. Similarly, you have to type a # in order to express decimal numbers. You can
  45. use the conversion method just described (type in the number, hit return) to
  46. find out a decimal number√ïs corresponding hexadecimal number and ASCII
  47. character.
  48.  
  49. What Was My Monitor Size? Here√ïs an impresive way to show a friend how many
  50. pixels horizontally and vertically they have on their monitor (other than
  51. looking at the manual, or something silly like that). Drop into MacsBug and
  52. type in: dm @@MainDevice GDevice. This will show you the struct members of
  53. the MainDevice (which happens to be a GDevice), you should see the gdPMap
  54. indented, three lines below it will be bounds with four numbers to the right
  55. of it. These four numbers are the top, left, bottom and right coordinates of
  56. the monitor, respectively.
  57.  
  58. The dm command is short for display memory, after you type in dm type in the
  59. address of the memory you want to display. MainDevice is a system global
  60. that is a handle (a pointer to a pointer) to the main graphics device (the
  61. one with the menu bar). The two @@ symbols are how you express
  62. double-indirection in MacsBug, in C you use "*" to express indirection (i.e.
  63. de-referencing) which is in put in prefix notation. People who program in
  64. Pascal can use the postfix indirection notation by saying "dm MainDevice^^
  65. GDevice".
  66.  
  67. After you give the dm command the address, you give it the format you want
  68. to see it diplayed as. You can use any number for the number of bytes you
  69. want displayed, or you can say "Rect", for instance, to see the first eight
  70. bytes of the memory in the form of a rectangle. You can also use: Byte,
  71. Word, Long, SignedByte, SignedWord, SignedLong, UnsignedByte, UnsignedWord,
  72. UnsignedLong, PString, CString, and PixMap, GDevice, RGBColor, CGrafPort and
  73. any number of other templates you may have installed.
  74.  
  75. Example: if you know a rectangle is at address $00058EA6 and you want to see
  76. what its value is, all you have to do is type in "dm $00058EA6 Rect".
  77.  
  78. By the way√ëA template a layout of memory that MacsBug knows about (such as a
  79. C struct or a Pascal record), you can type "tmp" to find out all of the
  80. templates your version of MacsBug has.
  81.  
  82. Don't you hate it when you are working in an application, minding your own
  83. business, when all-of-a-sudden the program quits and the system tells you an
  84. error of type X occured? There are many applications made where you can look
  85. up these numbers and find out what went wrong. MacsBug can also do this, all
  86. you have to do is type error and then the error number. Keep in mind that
  87. the error numbers the system gives you are decimal (not hexadecimal), so you
  88. should put a "#" in front of them.
  89.  
  90. Example: The sytem tells you: "An error of type 4 has occured," drop into
  91. MacsBug and type "error√ä#4", MacsBug will then output
  92. "$0004Ê#4ÊzeroÊdivideÊerror".
  93.  
  94. Note: This error feature is not in earlier versions of MacsBug, so you may
  95. not have it.
  96.  
  97. The Simple Calculator You can use MacsBug as a simple calucator. Let√ïs say
  98. you need to know what seven times seventeen is, type in "#7√ä*√ä#17", and hit
  99. return. The number 119 should now be on your screen. It will be hidden in
  100. the line:
  101.  
  102. #7 * #17 = $00000077 #119 #119 '¬•¬•¬•w'
  103.  
  104. The lower case letter w is the 119th ASCII character, as the previous line
  105. shows us. Let√ïs try another example, how about five plus six? You would type
  106. in "#5 + #6", and hit return. You should then see:
  107.  
  108. #5 + #6 = $0000000B #11 #11 '¬•¬•¬•¬•'
  109.  
  110. MacsBug can also handle multiple operations at a time, like five plus six
  111. plus ten. If you want to say something like five plus six times four
  112. remember to put parentheses around the apropiate numbers. MacsBug has no
  113. concept of orders of operations and it√ïs quite possible for it to add before
  114. it multiplies. So say this: "#5√ä+ (#6√ä*√ä#4)", which equals #29, instead of
  115. "#5√ä+√ä#6√ä*√ä#4", which equals #44.
  116.  
  117. You can use +, -, *, /, MOD for arithmetic operations. You can use AND (or
  118. &), OR (or |), NOT (or !), XOR for boolean operations. And you can use = (or
  119. ==), <> (or !=), <, >, <=, >= for equality operations.
  120.  
  121. If you type in "#5√ä+√ä#4√ä=√ä#9" MacsBug will give you a one, meaning that the
  122. equality you just said was true. If you said "#5√ä+√ä#4√ä=√ä#10", Macsbug will
  123. give you a zero, meaning that the equality five plus four equals ten is
  124. false.
  125.  
  126. Moving the Cursor Here is a cool trick to move the cursor. It done by
  127. setting memory, the mouse tracking variables specifically. But I√ïd like to
  128. talk about setting memory beforehand. There are four commands in MacsBug to
  129. set memory: SB (set byte), SW (set word), SL (set long), and SM (set
  130. memory). You give each of these commands an address first, and then the
  131. values of what you want to set the memory to. Example: There is a byte that
  132. you have the address of that you want to set to ten, you should type in:
  133.  
  134. SB $XXXXXXXX #10
  135.  
  136. where $XXXXXXXX is the address of the byte. Another example: There is a long
  137. that you have the address of that you want to set to "$4D616320", you should
  138. type in:
  139.  
  140. SL $XXXXXXXX $4D616320
  141.  
  142. again, where $XXXXXXXX is the address of the long. You can use the SM
  143. command the same way in the case that the length you want to set is not 1, 2
  144. or 4 bytes long. You can use SW when you want to set a word (2 bytes).
  145.  
  146. If you are familiar with Points (the vertical and horizontal coordinates of
  147. a point on the graf plane), you should know that they take up four bytes in
  148. memory. The high two bytes (the high word) is the vertical coordinate, and
  149. the low two bytes (the low word) is the horizontal coordinate. There are two
  150. global variables that are both Points, one called MTemp, the other called
  151. RawMouse, these variables are the information the Macintosh uses for
  152. controling the cursor. You can set these points by using SL.
  153.  
  154. There is also a byte called CrsrNew, set this byte to 1 when you want to
  155. notify the Macintosh that the cursor posistions have changed. This is how
  156. you move the mouse to point (5,√ä6), near the upper-left corner of the
  157. screen:
  158.  
  159. SL MTemp $00060005 SL RawMouse $00060005 SB CrsrNew #1
  160.  
  161. Make sure MTemp and RawMouse have the same value. Now type Command-G to see
  162. your newly moved cursor.
  163.  
  164. Recovering from a Hung Serial Port
  165. Sometimes when you're AppleTalking or modeming and something goes wrong
  166. (like you switch the modem off while data is being sent to it), the comptuer
  167. will hang. The mouse will still move, but clicking will have no effect.
  168. Here's the solution:
  169.  
  170. Drop into MacsBug. You should see the routine name "_vSyncWait" plus
  171. something as the current location. If you don't, you probably hit the system
  172. while it was doing something else. Hit Command-G to get back out of MacsBug,
  173. and try again. After a few tries you should find _vSyncWait.
  174.  
  175. _vSyncWait is the routine that the system uses to wait for some input from a
  176. serial port. If you can read assembly code, you√ïll see that it√ïs pretty
  177. simple. Here√ïs the dump of the significant part:
  178.  
  179. +0000 4080BB8C MOVE.W $0010(A0),D0 |
  180. 3028 0010
  181. +0004 4080BB90 BGT.S _vSyncWait ; 4080BB8C |
  182. 6EFA
  183.  
  184. Register A0 is pointing to a system data structure, in which a word will be
  185. cleared when the awaited input arrives. The MOVE.W instruction grabs this
  186. word and puts it into register D0. The BGT.S instruction then Branches back
  187. to the MOVE.W if the byte it just fetched is Greater Than zero. So it
  188. happens that this byte is never going to arrive for whatever reason, but the
  189. computer is going to wait for eternity. The secret to fixing this is to use
  190. Command-T to go step along until the MOVE.W instruction is displayed as the
  191. current instruction. Now use the sw command to set "@(A0+10)" to zero:
  192.  
  193. sw @(A0+10) 0
  194.  
  195. Hit Command-T twice more. The MOVE.W instruction will take the zero you just
  196. set into memory and put it in D0, so the D0 display on the left of the
  197. screen should have its right four digits all zeros. Then when you execute
  198. the BGT.S instruction, it should not go back to the MOVE.W since zero is not
  199. greater than zero.
  200.  
  201. Hit Command-G to go. If this was the only byte the software was waiting for,
  202. then it should continue running, although it may go a little crazy because
  203. it's been suddenly disconnected from whatever peripheral it was talking to.
  204. Quit the program, fix your hardware, and try again.
  205.  
  206. Shameless Strobe Light Trick
  207. Okay, this is a really useless trick, but it√ïs cool for at least a little
  208. while. Go into MacsBug. If you have a single screen type in "swap", the
  209. console should then say "Display will be swapped after each trace or step",
  210. if it doesn√ït type in "swap" again. Swapping is when the screen switches
  211. from the MacsBug console to the normal Macintosh screen. We want it to swap
  212. after each trace or step, which is what we just did up above. Now we need it
  213. to step, thereby swapping the screen, the "s" command (the step command) is
  214. just what we need to do this. We want this to happend more than once, so we
  215. type in: "s 100", which steps 100 times. Enjoy the show.
  216.  
  217. Warning: Swapping with a number like 1000 can render some machines, like my
  218. PowerBook 165c, useless until it is all over with, so keep the numbers low
  219. or the patience high.
  220.  
  221. GetKeys from within MacsBug
  222. There is a routine in the Macintosh toolbox called GetKeys, this routine is
  223. great for game programmers who want a reasonably fast way to read the
  224. keyboard, without using (slower) events. The problem for C and C++
  225. programmers using this routine is that the KeyMap type is a Pascal packed
  226. array. Each bit of the packed array is designated to a certain key, the bit
  227. is set to 1 if the key is down, and set to zero if the key is up. This array
  228. takes up 16 bytes (128 bits). C cannot access the elements of the packed
  229. array like a normal array, so the programmer has to mask out some bits to
  230. get the result that he/she wants. There is a desk accessory named "GetKeys,"
  231. that is made just for this case. The problem is, you might not be on a
  232. machine with that program on it.
  233.  
  234. Good thing MacsBug is able to help us. Here is how you locate the bit for
  235. the letter "M": go into MacsBug and type in "dm KeyMap", but don't hit
  236. return just yet. Now strike the escape key, this should swap the screen.
  237. Press and hold down the letter "M" on your keyboard, this should swap the
  238. screen back. Now, while still holding down "M", press return. This is what
  239. you should see:
  240.  
  241. Displaying memory from 0174
  242. 00000174 0000 0000 0040 0000 0000 0000 0000 0000¬•¬•¬•¬•¬•@¬•¬•¬•¬•¬•¬•¬•¬•¬•¬•
  243.  
  244. The number "00000174" is the address of the KeyMap global variable. The next
  245. set of numbers√í0000√ä0000√ì is the first element of the C version of the
  246. array, in other words, it√ïs: "keyMap[0]". The next set of numbers
  247. "0040√ä0000" is the second element of the array, keyMap[1]. The next group of
  248. 8 hexadecimal digits is the third element (keyMap[2]), and the last group of
  249. 8 hexadecimal digits is the fourth element (keyMap[3]). The series of
  250. bullets is what the array looks like in ASCII form. In the second group
  251. ("0040√ä0000") there is a 4 in the midst of all of those zeros. This is the
  252. bit that is set to 1 whenever the "M" key is held down. So, to see if the
  253. "M" is down from within C we will do this:
  254.  
  255. KeyMap keyMap;
  256. GetKeys( keyMap );
  257.  
  258. if( keyMap[1] & 0x00400000 )
  259. { DoMKeyDown();
  260. }
  261.  
  262. The Lost Paper
  263. I was once typing in some text in a word processor, when the computer
  264. suddenly crashed on me. I didn't save a copy on to the hard-disk yet. I had
  265. to restart the computer and type it all over again. But wait, the paper is
  266. still in the machine I thought to myself. You see, when you restart, all of
  267. the computer√ïs memory doesn't get cleared, it just stays to what it was
  268. until it gets replaced with other information, much like the behavior of a
  269. hard-disk. I had one thing going for me, I had MacsBug installed. Here are
  270. the steps I took to recover the paper:
  271.  
  272. First, I logged all of the work I was doing in MacsBug to a file. I did this
  273. using the log command. All you need to give the log command is the name of
  274. the new file to log to. I named it MyPaper. Good, now all of my MacsBug
  275. session will be on the hard-disk so I can open it up with a normal text
  276. editor when I√ïm done.
  277.  
  278. Next, I needed to find where in memory my paper was. I did this using the
  279. "f" command. The first two parameters for this command is the range in
  280. memory in which you want MacsBug to search through. I wanted to search
  281. through all of my memory, which is 8 megs on my machine, so I typed in:
  282. "f√ä0√ä(400√ä*√ä400√ä*√ä8)√ä"any string". Where 0 is the beginning of memory and 8
  283. megs is the top of it. (Note: "400√ä*√ä400" is exactly one megabyte of
  284. memory.) The last parameter is the search string, balanced between two
  285. single quotes. I wanted to pick a distinct string, otherwise I would have
  286. found other parts of memory, which would take longer to do. I knew the most
  287. famous mammal, the aardvark, was mentioned in my paper, so I typed in this
  288. for the find command:
  289.  
  290. f 0 (400 * 400 * 8) "aardvark"
  291.  
  292. MacsBug then started searching for me. It came up with a small memory dump
  293. of something with the word arrdvark in it, but the words after it were not
  294. mine, which meant that I found another part of memory instead of my paper. I
  295. hit return to tell MacsBug to keep on searching. MacsBug then came up with a
  296. dump from my paper:
  297.  
  298. Searching for "aardvark" from 00000000 to 007FFFFF
  299. 00358200 6161 7264 7661 726B 8000 0000 0000 002C aardvark answer, Which was
  300. very good news indeed! This told me that the string "aardvark answer" could
  301. be found at address 00358200. (I got this address from the leftmost number
  302. given.)
  303.  
  304. Now that I knew where it was, the rest my task would be easy. I used
  305. MacsBug√ïs display ASCII command to show me what came after it, by typing in:
  306. dma 00358200. You might not have this command, in which case you√ïll have to
  307. use plain old dm, instead of dma. I hit return until my paper was done being
  308. displayed.
  309.  
  310. Note: You can subract a number from the address to see what was before it.
  311.  
  312. I then typed in "log" again, which closed my log. Finally, I went out of
  313. MacsBug and opened the log file with SimpleText. Remember, the log had my
  314. whole session not just the paper so I had to delete the addresses and such
  315. from it, which really isn't that hard to do, if you know how to use your
  316. mouse and your delete key efficiently. The paper was saved.
  317.  
  318. Warning: In your search you might stumble upon MacsBug√ïs very own memory,
  319. with its own copy of your search string. To get out of this loop, start the
  320. search over again with the base search address being outside of MacsBug√ï's
  321. memory. Credits
  322. The authors of this are Macneil Shonle and Dustin Mitchell of The Syzygy
  323. Cult, a programming group that makes games and utilities. Dustin reviewed
  324. this and submitted the Recovering from a Hung Serial Port section, thanks
  325. Dustin!
  326.  
  327. Email MacneilS@aol.com if you have some input on how I can make Cool MacsBug
  328. Tricks even better; nothing is too small to mention. Thanks for reading!
  329.  
  330. ----------------------------------------------------------------------------
  331. Copyright¬© 1995, Macneil Shonle. All rights reserved.